package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.constant.MessageConstant;
import com.itcast.entity.Result;
import com.itcast.service.MemberService;
import com.itcast.service.SetmealService;
import com.itcast.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/rport")
public class ReportController {

    @Reference
    private MemberService memberService;

    @Reference
    private SetmealService setmealService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        try {
            Map<String,Object> data = new HashMap<>();
//            获取日历对象，时间为系统时间
            Calendar calendar = Calendar.getInstance();
//            设置时间
            calendar.set(2019,5,28);
//            往前推移12个月
            calendar.add(Calendar.MONTH,-12);
            List<String> months = new ArrayList<>();
            for (int i = 0; i < 11; i++) {
//                往后推一个月
                calendar.add(Calendar.MONTH,1);
//                转换成date类型
                Date month = calendar.getTime();
//                按照指定格式转化成字符串，并保存到集合
                months.add(new SimpleDateFormat("yyyy-MM").
                        format(month));
            }
            List<Integer> memberCount=memberService.
                    findMemberCountBymonth(months);
            data.put("months",months);
            data.put("memberCount",memberCount);
            return new Result(true,
                    MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,
                    MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        try {
            List<Map<String, Object>> setmealCount = setmealService.findSetmealCount();
            List<String> setmealNames = new ArrayList<>();
            if (setmealCount!=null){
                for (Map<String, Object> map : setmealCount) {
                    String setmealName = (String) map.get("name");
                    setmealNames.add(setmealName);
                }
            }
            Map<String,Object> map = new HashMap<>();
            map.put("setmealNames",setmealNames);
            map.put("setmealCount",setmealCount);
            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }
}
